package poker;

import java.awt.Color;
import java.awt.Font;
import java.util.Scanner;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.*;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;


public class HumanGui extends PlayerGui {
	private static final long serialVersionUID = 1L;
	Color background = new Color(0, 128, 128);
	String name;
	public HumanGui(String name, int i)	{
		setBackground(new Color(0, 139, 139));
		setFocusable(false);
		setLayout(null);
		switch(i){
	      case 0:
		    setBounds(192-160, 325, 240, 150);
		    break;
	      case 1:
	    	setBounds(192-160, 32, 240, 150);
		    break;
	      case 2:
		    setBounds(192+160, 32, 240, 150);
		    break;
	      case 3:
	    	setBounds(192+160, 325, 240, 150);
		    break;
	    
		}
	}
	
	 /** ZWRACANIE NAZWY GRACZA. */
	 public String getName(){
	   return("" + name + "");
	 }
	  
	  /** ZWRACANIE WARTOSCI Z PRZEDZIALU [a,b] (WYBRANEJ PRZEZ GRACZA). */
	 // int getValue(int a, int b){
	   // int input=(-1);
	    //Scanner keyboard=new Scanner(System.in);
	    //try{
		 // do{ /** WCZYTUJEMY WARTOSC Z PRZEDZIALU [a,b]. */
		  //  System.out.print("[" + playerName + "]" + " Choose value (" + a + "-" + b + "): ");
		    //input=keyboard.nextInt();
		  //} while((input<a)||(input>b));
	    //} catch (Exception e){System.out.println("[" + playerName + "]" + " INVALID INPUT"); System.exit(0);} /** DLA NIEPOPRAWNEJ WARTOSCI PRZERYWAMY DZIALANIE. */
	    //return(input);
	  //}

	  /** ZWRACANIE TABLICY KART DO WYMIANY (WYBRANYCH PRZEZ GRACZA). */
	  int[] exchangeCards(final HandGui hand){
		  
		  final int[] toExchange;
			
		  
			int i, count = 0;
			for (i = 0; i < 5; i++)	{
				if (hand.Cards[i].hideornot())
						count ++;
			}
			//System.out.println("count:"+count);
			toExchange = new int[count];
			int j=0;
			for (i = 0; i < 5;i++ )	{
				if (hand.Cards[i].hideornot())	{
						toExchange[j]=i+1;
						j++;
						//System.out.println("i:"+i);
					
				}		
			}

		return toExchange;
	  }

	  /** ZWRACANIE NUMERU AKCJI W RUNDZIE LICYTACJI (WYBRANEJ PRZEZ GRACZA). */
	  /** 1-Check, 2-Bet, 3-Raise, 4-Call, 5-Fold, 6-All-In. */
	  int betAction(final TableGui T,final Table table, boolean noBet, boolean allIn, boolean canCall, boolean canRaise){
	    /** MOZNA ZAGRAC: Check, Bet, Fold, All-In. */
		if(noBet){
			System.out.println(" (1) Check  (2) Bet  (3) Fold  (4) All-In ");
		final JButton CheckButton = new JButton("CHECK");
		CheckButton.setBounds(0,64,120,30);
		CheckButton.setFont(new Font("Tahoma", Font.BOLD, 11));
		CheckButton.setForeground(new Color(255, 140, 0));
		//CheckButton.setBackground(background);
		
		final JButton BetButton = new JButton("BET");
		BetButton.setBounds(120,64,120,30);
		//RaiseButton.setBackground(background);
		BetButton.setFont(new Font("Tahoma", Font.BOLD, 11));
		BetButton.setForeground(new Color(255, 140, 0));
		
		final JButton FoldButton = new JButton("FOLD");
		FoldButton.setBounds(0, 94, 120, 30);
		FoldButton.setFont(new Font("Tahoma", Font.BOLD, 11));
		FoldButton.setForeground(new Color(0, 0, 0));

		//FoldButton.setBackground(background);
		
		final JButton AllInButton = new JButton("ALL IN");
		AllInButton.setBounds(120,94,120,30);
		AllInButton.setFont(new Font("Tahoma", Font.BOLD, 11));
		AllInButton.setForeground(new Color(255, 0, 0));
		//AllInButton.setBackground(background);
		add(FoldButton);
		add(BetButton);
		add(CheckButton);
		add(AllInButton);
		//TableGui.class.LoadValue(table.numberOfCurrentPlayer);
		CheckButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				remove(FoldButton);
				remove(BetButton);
				remove(CheckButton);
				remove(AllInButton);repaint();
				LoadValue(T,table,1);

				
			}
		});
		 BetButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				remove(FoldButton);
				remove(BetButton);
				remove(CheckButton);
				remove(AllInButton);repaint();
				LoadValue(T,table,2);

			
			}
		});
		 FoldButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					remove(FoldButton);
					remove(BetButton);
					remove(CheckButton);
					remove(AllInButton);repaint();
					LoadValue(T,table,5);

				
				}
			});
		AllInButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					remove(FoldButton);
					remove(BetButton);
					remove(CheckButton);
					remove(AllInButton);repaint();
					LoadValue(T,table,6);

				
				}
			});
		
		}
		/** MOZNA ZAGRAC: Raise, Call, Fold, All-In. */
	else if((!noBet)&&(!allIn)&&(canCall)&&(canRaise)){
		System.out.println(" (1) Raise  (2) Call  (3) Fold (4) All-In ");
		
		final JButton RaiseButton = new JButton("RAISE");
		RaiseButton.setBounds(0,64,120,30);
		RaiseButton.setFont(new Font("Tahoma", Font.BOLD, 11));
		RaiseButton.setForeground(new Color(255, 140, 0));
		//CheckButton.setBackground(background);
		final JButton CallButton = new JButton("CALL");
		CallButton.setBounds(120,64,120,30);
		//RaiseButton.setBackground(background);
		CallButton.setFont(new Font("Tahoma", Font.BOLD, 11));
		CallButton.setForeground(new Color(255, 140, 0));
		final JButton FoldButton = new JButton("FOLD");
		FoldButton.setBounds(0, 94, 120,30);
		FoldButton.setFont(new Font("Tahoma", Font.BOLD, 11));
		FoldButton.setForeground(new Color(0, 0, 0));
		//FoldButton.setBackground(background);
		final JButton AllInButton = new JButton("ALL IN");
		AllInButton.setBounds(120,94,120,30);
		AllInButton.setFont(new Font("Tahoma", Font.BOLD, 11));
		AllInButton.setForeground(new Color(255, 0, 0));
		//AllInButton.setBackground(background);
		add(FoldButton);
		add(CallButton);
		add(RaiseButton);
		add(AllInButton);
		RaiseButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				remove(FoldButton);
				remove(CallButton);
				remove(RaiseButton);
				remove(AllInButton);
				repaint();
				LoadValue(T,table,3);

			}
		});
		CallButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				remove(FoldButton);
				remove(CallButton);
				remove(RaiseButton);
				remove(AllInButton);
				repaint();
				LoadValue(T,table,4);

			
			}
		});
		 FoldButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					remove(FoldButton);
					remove(CallButton);
					remove(RaiseButton);
					remove(AllInButton);
					repaint();
					LoadValue(T,table,5);

				}
			});
		AllInButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					remove(FoldButton);
					remove(CallButton);
					remove(RaiseButton);
					remove(AllInButton);
					repaint();
					LoadValue(T,table,6);

				
				}
			});
		}
		/** MOZNA ZAGRAC: Call, Fold, All-In. */
		else if((!noBet)&&(!allIn)&&(canCall)&&(!canRaise)){
		System.out.println(" (1) Call  (2) Fold  (3) All-In ");
		final JButton CallButton = new JButton("CALL");
		CallButton.setBounds(0,64,240,30);
		CallButton.setFont(new Font("Tahoma", Font.BOLD, 11));
		CallButton.setForeground(new Color(255, 140, 0));
		//CheckButton.setBackground(background);
		
		final JButton FoldButton = new JButton("FOLD");
		FoldButton.setBounds(0,94,120,30);
		FoldButton.setFont(new Font("Tahoma", Font.BOLD, 11));
		FoldButton.setForeground(new Color(255, 0, 0));
		//AllInButton.setBackground(background);
		final JButton AllInButton = new JButton("ALL IN");
		AllInButton.setBounds(120,60,120,30);
		AllInButton.setFont(new Font("Tahoma", Font.BOLD, 11));
		AllInButton.setForeground(new Color(255, 0, 0));
		//AllInButton.setBackground(background);
		add(FoldButton);
		add(CallButton);
		add(AllInButton);
		CallButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				remove(FoldButton);
				remove(CallButton);
				remove(AllInButton);repaint();
				LoadValue(T,table,4);


			}
		});
		 FoldButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				remove(FoldButton);
				remove(CallButton);
				remove(AllInButton);repaint();
				LoadValue(T,table,5);

			
			}
		});
		AllInButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					remove(FoldButton);
					remove(CallButton);
					remove(AllInButton);repaint();
					LoadValue(T,table,6);

				
				}
			});
		}
		/** MOZNA ZAGRAC: Fold, All-In. */
		else if((!noBet)&&(!allIn)&&(!canCall) || allIn){
		System.out.println(" (1) Fold  (2) All-In ");
		final JButton FoldButton = new JButton("FOLD");
		FoldButton.setBounds(0,64,120,30);
		FoldButton.setFont(new Font("Tahoma", Font.BOLD, 11));
		FoldButton.setForeground(new Color(255, 0, 0));
		//AllInButton.setBackground(background);
		final JButton AllInButton = new JButton("ALL IN");
		AllInButton.setBounds(120,64,120,30);
		AllInButton.setFont(new Font("Tahoma", Font.BOLD, 11));
		AllInButton.setForeground(new Color(255, 0, 0));
		//AllInButton.setBackground(background);
		add(FoldButton);
		add(AllInButton);
		 FoldButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					remove(FoldButton);
					remove(AllInButton);repaint();
					LoadValue(T,table,5);
				
				}
			});
		AllInButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				remove(FoldButton);
				remove(AllInButton);repaint();
				LoadValue(T,table,6);
	
			}
		});
		 
		}
	  return 0;
		
	  }

	  

	  public void LoadValue (TableGui T,Table table, int i)	{
			
		  switch(i){
	      case 1:
		    System.out.println(table.players[table.numberOfCurrentPlayer].getName() + "plays CHECK");
			   T.endbetTurn(table.numberOfCurrentPlayer);
		    break;
          case 2:
        	//  table.betValue;//=players[numberOfCurrentPlayer].
        	  getValue(1,T,table,1,(table.playersChips[table.numberOfCurrentPlayer]-1));
        /*	  System.out.println(table.players[table.numberOfCurrentPlayer].getName() + " plays BET (" + table.betValue + ")");
  		    table.tablePool+=table.betValue;
  		    table.playersChips[table.numberOfCurrentPlayer]-=table.betValue;
  		    table.playersBetsThisTurn[table.numberOfCurrentPlayer]+=table.betValue;
  		    if(table.betValue>table.highestBid){table.highestBid=table.betValue;}
  		    table. actionsPerformed++;*/
		    break;
	      case 3:
	    	//  table.betValue;//=players[numberOfCurrentPlayer].
	    	  getValue(2,T,table,(table.highestBid+1),(table.playersChips[table.numberOfCurrentPlayer]-1));
		  /*  System.out.println(table.players[table.numberOfCurrentPlayer].getName() + " plays RAISE (" +   table.betValue + ")");
		    table.tablePool+=(  table.betValue-table.playersBetsThisTurn[table.numberOfCurrentPlayer]);
		    table.playersChips[table.numberOfCurrentPlayer]-=(  table.betValue-table.playersBetsThisTurn[table.numberOfCurrentPlayer]);
		    table.playersBetsThisTurn[table.numberOfCurrentPlayer]+=(  table.betValue-table.playersBetsThisTurn[table.numberOfCurrentPlayer]);
		    table.highestBid=  table.betValue;
		    table.actionsPerformed=1;*/
		    break;
	      case 4:
	    	  table.betValue=table.highestBid;
		    System.out.println(table.players[table.numberOfCurrentPlayer].getName() + " plays CALL (" +   table.betValue + ")");
		    table.tablePool+=(  table.betValue-table.playersBetsThisTurn[table.numberOfCurrentPlayer]);
		    table.playersChips[table.numberOfCurrentPlayer]-=(  table.betValue-table.playersBetsThisTurn[table.numberOfCurrentPlayer]);
		    table.playersBetsThisTurn[table.numberOfCurrentPlayer]+=(  table.betValue-table.playersBetsThisTurn[table.numberOfCurrentPlayer]);
		    table.actionsPerformed++;
			   T.endbetTurn(table.numberOfCurrentPlayer);
		    break;
	      case 5:
		    System.out.println(table.players[table.numberOfCurrentPlayer].getName() + " plays FOLD");
		    table.playersActivity[table.numberOfCurrentPlayer]=0;
		    table.numberOfActivePlayers--;
			   T.endbetTurn(table.numberOfCurrentPlayer);
		    break;
	      case 6:
	    	  table.betValue=table.playersChips[table.numberOfCurrentPlayer];
		   System.out.println(table.players[table.numberOfCurrentPlayer].getName() + " plays ALL-IN (" + (  table.betValue+table.playersBetsThisTurn[table.numberOfCurrentPlayer]) + ")");
		    table.tablePool+=  table.betValue;
		    table.playersChips[table.numberOfCurrentPlayer]-=  table.betValue;
		    table.playersBetsThisTurn[table.numberOfCurrentPlayer]+=  table.betValue;
		    if(table.betValue>table.highestBid){table.highestBid=  table.betValue;}
		    if(table.numberOfAllIns==0) table.actionsPerformed=0;
		    table.playedAllIn=true;
		    table.numberOfAllIns++;
		    table.actionsPerformed++;
			   T.endbetTurn(table.numberOfCurrentPlayer);
		    break;
        }
		System.out.println();
      
	  	}
	  
	  int getValue(int i,final TableGui T,final Table table,int a, int b) {
		final JSlider slider = new JSlider();
		slider.setValue((int)(a-b)/2);
		slider.setMinimum(a);
		slider.setMaximum(b);
		slider.setBounds(60,64,120,30);
		slider.setBackground(background);
		add(slider);
		
		final JTextField actual = new JTextField(""+slider.getValue());
		actual.setBackground(background);
		actual.setFont(new Font("Tahoma", Font.BOLD, 11));
		actual.setBounds(180,64,30,30);
		add(actual);
		
		switch(i){
			case 1:{

				final JButton OK = new JButton("OK");
				OK.setBackground(new Color(240, 240, 240));
				OK.setFont(new Font("Tahoma", Font.BOLD, 9));
				OK.setBounds(0,64,60,30);
				add(OK);
		OK.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
	  		    table.betValue = Integer.parseInt(actual.getText());
				System.out.println(table.players[table.numberOfCurrentPlayer].getName() + " plays BET (" + table.betValue + ")");
		  		    table.tablePool+=table.betValue;
		  		    table.playersChips[table.numberOfCurrentPlayer]-=table.betValue;
		  		    table.playersBetsThisTurn[table.numberOfCurrentPlayer]+=table.betValue;
		  		    if(table.betValue>table.highestBid){table.highestBid=table.betValue;}
		  		    table.actionsPerformed++;
					 //  table.PlayerMove[table.numberOfCurrentPlayer]=true;
					 //  table.PlayerMove[table.numberOfCurrentPlayer+1]=false;
					   T.endbetTurn(table.numberOfCurrentPlayer);
					   remove(OK);
				remove(slider);
				remove(actual); 
				repaint();
			}
		});
		actual.addActionListener(new ActionListener() {
	        @Override
	        public void actionPerformed(ActionEvent e) {
		            	slider.setValue(Integer.parseInt(actual.getText()));
		            	table.betValue = Integer.parseInt(actual.getText());
	        }
			});
		slider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                actual.setText(String.valueOf(slider.getValue()));
            }
		});
		 this.addFocusListener(new FocusListener() {
		        @Override
		        public void focusLost(FocusEvent e){
		        }

		        @Override
		        public void focusGained(FocusEvent e) {
		      
		        	actual.setText(String.valueOf(slider.getValue()));
		        
		        }
		    });
		} break;
			case 2:{

				final JButton OK = new JButton("OK");
				OK.setBackground(new Color(240, 240, 240));
				OK.setFont(new Font("Tahoma", Font.BOLD, 9));
				OK.setBounds(0,64,60,30);
				add(OK);
				OK.addActionListener(new ActionListener(){
					@Override
					public void actionPerformed(ActionEvent e){
			  		    table.betValue = Integer.parseInt(actual.getText());
			  		  System.out.println(table.players[table.numberOfCurrentPlayer].getName() + " plays RAISE (" +   table.betValue + ")");
					    table.tablePool+=(  table.betValue-table.playersBetsThisTurn[table.numberOfCurrentPlayer]);
					    table.playersChips[table.numberOfCurrentPlayer]-=(  table.betValue-table.playersBetsThisTurn[table.numberOfCurrentPlayer]);
					    table.playersBetsThisTurn[table.numberOfCurrentPlayer]+=(  table.betValue-table.playersBetsThisTurn[table.numberOfCurrentPlayer]);
					    table.highestBid= table.betValue;
					    table.actionsPerformed=1;
					//   table.PlayerMove[table.numberOfCurrentPlayer]=true;
					   T.endbetTurn(table.numberOfCurrentPlayer);
						remove(OK);
						remove(slider);
						remove(actual); repaint();
					}
				});
				actual.addActionListener(new ActionListener() {
			        @Override
			        public void actionPerformed(ActionEvent e) {
				            	slider.setValue(Integer.parseInt(actual.getText()));
				            	table.betValue = Integer.parseInt(actual.getText());
			        }
					});
				slider.addChangeListener(new ChangeListener() {
		            @Override
		            public void stateChanged(ChangeEvent e) {
		                actual.setText(String.valueOf(slider.getValue()));
		            }
				});
				 this.addFocusListener(new FocusListener() {
				        @Override
				        public void focusLost(FocusEvent e){
				        }

				        @Override
				        public void focusGained(FocusEvent e) {
				      
				        	actual.setText(String.valueOf(slider.getValue()));
				        
				        }
				    });
			} break;
		} 
		 return 0;
		 
	  	}
}